import Toast from '@vant/weapp/toast/toast';

const app = getApp();
const db = wx.cloud.database();
const _ = db.command;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type: '',
    haveSkechList: [],
    haveLearnList: [],
    haveColorList: []
  },
  // 预览线稿、参考图片、生成图片
  previewSkech(e) {
    var skechIndex = e.currentTarget.dataset.index;
    var haveSkechList = this.data.haveSkechList;
    wx.previewImage({
      urls: haveSkechList,
      current: haveSkechList[skechIndex]
    })
  },

  previewLearn(e) {
    var learnIndex = e.currentTarget.dataset.index;
    var haveLearnList = this.data.haveLearnList;
    wx.previewImage({
      urls: haveLearnList,
      current: haveLearnList[learnIndex]
    })
  },

  previewColor(e) {
    var colorIndex = e.currentTarget.dataset.index;
    var haveColorList = this.data.haveColorList;
    wx.previewImage({
      urls: haveColorList,
      current: haveColorList[colorIndex]
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    console.log(options);
    var myOpenId = app.globalData.openid
    this.setData({
      type: options.type
    })
    console.log(that.data.type)
    db.collection('userInfo').where({
      _openid: myOpenId
    }).get({
      success(res) {
        console.log(res.data[0])
        if (that.data.type == "skech" && res.data[0].haveSkechs.length == 0) {
          setTimeout(() => {
            wx.navigateBack({
            delta: 1,
          })
          }, 2000);
          Toast.fail("未加工过线稿")
        }
        if (that.data.type == "learn" && res.data[0].haveLearnPics.length == 0) {
          setTimeout(() => {
            wx.navigateBack({
              delta: 1,
            })
          }, 2000);
          Toast.fail("未加工过参考图片")
        }
        if (that.data.type == "coloization" && res.data[0].haveColoizations.length == 0) {
          setTimeout(() => {
            wx.navigateBack({
              delta: 1,
            })
          }, 2000);
          Toast.fail("未生成过风格转换图片")
        }
        that.setData({
          haveSkechList: res.data[0].haveSkechs,
          haveLearnList: res.data[0].haveLearnPics,
          haveColorList: res.data[0].haveColoizations
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})